package com.fs.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.*;
import com.fs.pojo.*;
import com.fs.service.*;
import com.fs.vo.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yyds
 * @fileName InStoServiceImpl
 * @date 2024/10/18 15:13
 * @description 入库申请服务层
 **/
@Service
public class InStoServiceImpl implements InStoService {
    @Resource
    private InStoMapper inStoMapper;
    @Resource
    private WhWarehousingMapper whWarehousingMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private ProcurementService procurementService;
    @Resource
    private WhDetailedMapper whDetailedMapper;
    @Resource
    private ReserveMapper reserveMapper;
    @Resource
    private RtaService rtaService;
    @Resource
    private BusinessService businessService;

    /**
     * 分页查询入库申请
     *
     * @param condition
     * @return date
     */
    @Override
    public PageInfo<InSto> selectAll(Condition<InSto> condition) {
        PageHelper.startPage(condition.getPage(), condition.getCount());
        List<InSto> list = inStoMapper.findAll(condition.getSearch());
        return new PageInfo<>(list);
    }

    @Override
    public Integer addInSto(InSto inSto) {
        return inStoMapper.insert(inSto);
    }


    /**
     * 新增入库申请
     *
     * @param condition
     * @return yes/no
     */


    /**
     * 根据编号删除入库申请
     *
     * @param id
     * @return yes/no
     */
    @Override
    public Boolean deleteInSto(Integer id) {
        return inStoMapper.deleteById(id) > 0;
    }

    /**
     * 根据编号修改入库申请
     *
     * @param condition
     * @return
     */

    @Override
    public Boolean updateInSto(Condition<InSto> condition) {
        return inStoMapper.updateById(condition.getSearch()) > 0;
    }

    /**
     * 入库申请审批
     *
     * @return
     */
    @Transactional
    @Override
    public Boolean approve(InSto inSto) {

        //更改状态
        Boolean commit = true;
        int result = inStoMapper.updateById(inSto);
        System.out.println(result);
        if (result == 0) {
            commit = false;
        }
        Double amount = 0.0;
        Sto sto = procurementService.queryProcurementById(inSto.getStoId());
        if (inSto.getInstoState() == 1) {
            QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("sto_pro_id", inSto.getStoId());
            List<OrderDetail> list = orderDetailMapper.selectList(wrapper);
            for (OrderDetail obj : list) {
                amount += obj.getPurchaseUnitPrice() * obj.getPurchaseQuantity();
            }
            //新增入库单
            WhWarehousing whWarehousing = new WhWarehousing();
            whWarehousing.setApproverId(1);
            whWarehousing.setApprovalDate(new Date());
            whWarehousing.setApprovalStatus(1);
            whWarehousing.setInspector(inSto.getInstoActor());
            whWarehousing.setTotal(list.size());
            whWarehousing.setReceiptDate(new Date());
            whWarehousing.setReceiptType("采购入库");
            whWarehousing.setTotalAmount(amount);
            whWarehousing.setWarehouseId(inSto.getInstoWarehouse());
            System.out.println(whWarehousing);
            result = whWarehousingMapper.insert(whWarehousing);
            System.out.println(result);
            if (result == 0) {
                commit = false;
            }
            //新增入库明细
            for (OrderDetail obj : list) {
                WhDetailed whDetailed = new WhDetailed();
                whDetailed.setQuantity(obj.getPurchaseQuantity());
                whDetailed.setUnitPrice(obj.getPurchaseUnitPrice());
                whDetailed.setVariantProductId(obj.getProductId());
                whDetailed.setWarehousingId(whWarehousing.getWarehousingEntryId());
                whDetailed.setProductName(obj.getProductName());
                whDetailed.setTotalPrice(obj.getPurchaseUnitPrice() * obj.getPurchaseQuantity());
                System.out.println(whDetailed);
                result = whDetailedMapper.insert(whDetailed);
                System.out.println(result);
                if (result == 0) {
                    commit = false;
                }
            }
            //入库
            for (OrderDetail obj : list) {
                Wh_inventory whInventory = new Wh_inventory();
                whInventory.setQuantity(obj.getPurchaseQuantity());
                whInventory.setWarehouseId(whWarehousing.getWarehouseId());
                System.out.println(obj.getProductId());
                whInventory.setProductId(obj.getProductId());
                result = reserveMapper.updateByCount(whInventory);
                if (result == 0) {
                    commit = false;
                }
            }
            //添加采购营业额
            Business business = new Business();
            business.setDate(new Date());
            business.setStoProId(sto.getStoProId());
            business.setStoType(1);
            business.setTotalMoney(-amount);
            result = businessService.addBusiness(business);
            if (result == 0) {
                commit = false;
            }
        } else {
            //退货
            Rta rta = new Rta();
            rta.setStoProId(sto.getStoProId());
            rta.setStoActor(sto.getStoActor());
            rta.setStoDate(new Date());
            rta.setStoNote("采购不通过退货");
            rta.setStoStatus(2);
            result = rtaService.addRta(rta);
            if (result == 0) {
                commit = false;
            }
        }
        return commit;
    }

}
